﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;


/// <summary>
/// 命令栈
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommandStack<T> where T: ICustomCommand
{
    Stack<T> commandStack;

    Stack<T> PopedCommandStack;
    
    public CommandStack()
    {
        commandStack = new Stack<T>();
        PopedCommandStack = new Stack<T>();
    }

    /// <summary>
    /// 撤销命令
    /// </summary>
    public void RedoCommand()
    {
        T temp = commandStack.Pop();
        if (temp!=null)
        {
            temp.Redo();
            PopedCommandStack.Push(temp);
        }
    }

    /// <summary>
    /// 执行（新）命令
    /// </summary>
    /// <param name="command"></param>
    public void DoCommand(T command)
    {
        command.Do();
        commandStack.Push(command);
        PopedCommandStack.Clear();
    }

    /// <summary>
    /// 执行（旧）命令，即之前执行过的命令
    /// </summary>
    public void DoNextCommand()
    {
        T temp = PopedCommandStack.Pop();
        if (temp!=null)
        {
            temp.Do();
            commandStack.Push(temp);
        }
    }
}
